#include "comm.hpp"

int main()
{
    //1.创建管道文件
    if(mkfifo(P_PATH.c_str(),0666))
    {
        perror("mkfifo");
        exit(1);
    }
    Log("创造管道文件成功",Debug) << "step 1" << std::endl;
    //2.打开管道文件
    int fd = open(P_PATH.c_str(),O_RDONLY);
    if(fd < 0)
    {
        perror("open");
        exit(2);
    }
    Log("打开管道文件成功",Debug) << "step 2" << std::endl;
    //3.读取
    char buffer[SIZE];
    while(true)
    {
        memset(buffer, '\0', sizeof(buffer));
        ssize_t s = read(fd, buffer, sizeof(buffer)-1);
        if (s > 0)
        {
            std::cout << "client say: " << buffer << std::endl;
        }
        else if(s == 0)
        {
            std::cout << "data finished" << std::endl; 
            break;
        }else{
            perror("read");
            break;
        }
    }

    //4.关闭文件
    close(fd);
    Log("关闭管道文件成功",Debug) << "step 3" << std::endl;
    //5.删除管道
    unlink(P_PATH.c_str());
    Log("删除管道文件成功",Debug) << "step 4" << std::endl;
    return 0;
}